Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
inquirer-autocomplete-prompt
Advanced tools
The inquirer-autocomplete-prompt npm package is an extension for the Inquirer.js library that provides an autocomplete prompt. It allows users to search and select from a list of options dynamically, which is particularly useful for large datasets or when the list of options is not known beforehand.
Basic Autocomplete Prompt
This feature allows users to search and select from a list of options dynamically. The code sample demonstrates a basic autocomplete prompt where users can type to filter through a list of fruits.
const inquirer = require('inquirer');
const autocompletePrompt = require('inquirer-autocomplete-prompt');
inquirer.registerPrompt('autocomplete', autocompletePrompt);
const questions = [
{
type: 'autocomplete',
name: 'fruit',
message: 'What is your favorite fruit?',
source: function(answersSoFar, input) {
input = input || '';
return new Promise(function(resolve) {
setTimeout(function() {
const fruits = ['Apple', 'Orange', 'Banana', 'Kiwi', 'Mango'];
const filtered = fruits.filter(fruit => fruit.toLowerCase().includes(input.toLowerCase()));
resolve(filtered);
}, 100);
});
}
}
];
inquirer.prompt(questions).then(answers => {
console.log('Your favorite fruit:', answers.fruit);
});
Dynamic Data Source
This feature allows the autocomplete prompt to fetch data from an external source dynamically. The code sample demonstrates how to use the GitHub API to search for repositories based on user input.
const inquirer = require('inquirer');
const autocompletePrompt = require('inquirer-autocomplete-prompt');
const axios = require('axios');
inquirer.registerPrompt('autocomplete', autocompletePrompt);
const questions = [
{
type: 'autocomplete',
name: 'repository',
message: 'Select a GitHub repository:',
source: async function(answersSoFar, input) {
input = input || '';
const response = await axios.get(`https://api.github.com/search/repositories?q=${input}`);
return response.data.items.map(repo => repo.full_name);
}
}
];
inquirer.prompt(questions).then(answers => {
console.log('Selected repository:', answers.repository);
});
Inquirer.js is a collection of common interactive command line user interfaces. It provides various types of prompts like input, confirm, list, rawlist, expand, checkbox, password, and editor. While it does not include an autocomplete prompt by default, it serves as the base library for inquirer-autocomplete-prompt.
Enquirer is a library for creating interactive CLI prompts. It offers a wide range of prompt types, including autocomplete, but is known for being more flexible and faster than Inquirer.js. Enquirer’s autocomplete prompt is similar to inquirer-autocomplete-prompt but is part of a more extensive set of features.
Prompt-sync is a synchronous prompt for node.js that is simple and easy to use. It does not support autocomplete functionality out of the box but can be extended with custom implementations. It is more lightweight compared to inquirer-autocomplete-prompt.
Autocomplete prompt for inquirer
npm install --save inquirer-autocomplete-prompt
This prompt is anonymous, meaning you can register this prompt with the type name you please:
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));
inquirer.prompt({
type: 'autocomplete',
...
})
Change autocomplete
to whatever you might prefer.
Note: allowed options written inside square brackets (
[]
) are optional. Others are required.
type
, name
, message
, source
[, pageSize
, filter
, when
, suggestOnly
, validate
]
See inquirer readme for meaning of all except source and suggestOnly.
Source will be called with previous answers object and the current user input each time the user types, it must return a promise.
Source will be called once at at first before the user types anything with null as the value. If a new search is triggered by user input it maintains the correct order, meaning that if the first call completes after the second starts, the results of the first call are never displayed.
suggestOnly is default false. Setting it to true turns the input into a normal text input. Meaning that pressing enter selects whatever value you currently have. And pressing tab autocompletes the currently selected value in the list. This way you can accept manual input instead of forcing a selection from the list.
validate is only active when suggestOnly is set to true. It behaves like validate for the input prompt.
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));
inquirer.prompt([{
type: 'autocomplete',
name: 'from',
message: 'Select a state to travel from',
source: function(answersSoFar, input) {
return myApi.searchStates(input);
}
}]).then(function(answers) {
//etc
});
See also example.js for a working example.
I recommend using this package with fuzzy if you want fuzzy search. Again, see the example for a demonstration of this.
ISC
FAQs
Autocomplete prompt for inquirer
The npm package inquirer-autocomplete-prompt receives a total of 1,011,277 weekly downloads. As such, inquirer-autocomplete-prompt popularity was classified as popular.
We found that inquirer-autocomplete-prompt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.